//
//  KATBanner.m
//  KATFramework
//
//  Created by Kantice on 15/9/11.
//  Copyright (c) 2015年 KatApp. All rights reserved.
//

#import "KATBanner.h"




@interface KATBanner ()

//数据数组
@property(nonatomic,retain) KATArray<__kindof KATBannerData *> *dataArray;

//指示器数组
@property(nonatomic,retain) KATArray<__kindof KATButton *> *indicatorArray;

//前景条
@property(nonatomic,retain) UIView *frontBar;

//远程图片更新定时器
@property(nonatomic,retain) NSTimer *remoteImageUpdateTimer;

//图片轮播定时器
@property(nonatomic,retain) NSTimer *changeImageTimer;

//是否快速拖动图片
@property(nonatomic,assign) BOOL isQuicklyPan;

//快速滑动方向
@property(nonatomic,assign) int quicklyPanDirection;

//容器
@property(nonatomic,retain) UIView *container;

//左图
@property(nonatomic,retain) KATImage *leftImage;

//中图（显示的图片）
@property(nonatomic,retain) KATImage *middleImage;

//右图
@property(nonatomic,retain) KATImage *rightImage;

//左图索引
@property(nonatomic,assign) int leftImageIndex;

//中图索引
@property(nonatomic,assign) int middleImageIndex;

//右图索引
@property(nonatomic,assign) int rightImageIndex;

@end



@implementation KATBanner


+ (instancetype)bannerWithFrame:(CGRect)frame
{
    KATBanner *banner=[[[self alloc] init] autorelease];
        
    banner.frame=frame;
    banner.clipsToBounds=YES;
    banner.backgroundColor=[UIColor clearColor];
    banner.state=BANNER_STATE_INIT;
    banner.changeMode=BANNER_CHANGE_MODE_LEFT;
    banner.isAutoChangeMode=NO;
    banner.stoped=NO;
    banner.animateTime=0.6f;
    banner.doubleClickCheckTime=0.4f;
    banner.isCycleMode=YES;
    banner.isQuicklyPan=NO;
    banner.quicklyPanDirection=BANNER_CHANGE_MIDDLE;
    banner.scaleMaxDefault=1.6;
    
    //图片轮播定时器
    banner.changeImageTimer=nil;
    banner.isChangeImageTimerWorking=NO;
    banner.changeImageInterval=6.0f;
    
    //远程图片更新定时器
    banner.hasRemoteImage=NO;
    banner.remoteImageUpdateTimer=nil;
    banner.isRemoteImageUpdateTimerWorking=NO;
    banner.remoteImageUpdateInterval=0.5;
    
    //容器
    banner.container=[[[UIView alloc] init] autorelease];
    banner.container.frame=CGRectMake(-frame.size.width, 0, frame.size.width*3, frame.size.height);//3倍尺寸
    banner.container.backgroundColor=[UIColor clearColor];
    [banner addSubview:banner.container];
    
    
    
    //双击事件
//    UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc] initWithTarget:banner action:@selector(doubleTap)];
//    doubleTap.numberOfTapsRequired=2;//点击次数
//    [banner.container addGestureRecognizer:doubleTap];//添加点击事件
    
    //点击事件
    UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget:banner action:@selector(imageTap:)];
    singleTap.numberOfTapsRequired=1;//点击次数
//    [singleTap requireGestureRecognizerToFail:doubleTap];//在双击事件失败后才执行
    [banner.container addGestureRecognizer:singleTap];//添加点击事件
    [singleTap release];
    singleTap=nil;
    
//    [doubleTap release];
//    doubleTap=nil;
    
    //拖动事件
    UIPanGestureRecognizer *panRecognizer=[[UIPanGestureRecognizer alloc] initWithTarget:banner action:@selector(actionPan:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [banner.container addGestureRecognizer:panRecognizer];
    [panRecognizer release];
    panRecognizer=nil;
    
    //捏合事件
    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:banner action:@selector(actionPinch:)];
    [banner.container addGestureRecognizer:pinch];
    [pinch release];
    
    //左中右图
    banner.leftImage=[KATImage image];
    banner.leftImage.frame=CGRectMake(0, 0, frame.size.width, frame.size.height);
    banner.leftImage.contentMode=UIViewContentModeScaleAspectFit;
    banner.leftImage.isParsingImageAsync=NO;
    [banner.leftImage removeTapGesture];
    [banner.container addSubview:banner.leftImage];
    
    banner.middleImage=[KATImage image];
    banner.middleImage.frame=CGRectMake(frame.size.width, 0, frame.size.width, frame.size.height);
    banner.middleImage.contentMode=UIViewContentModeScaleAspectFit;
    banner.middleImage.isParsingImageAsync=NO;
    [banner.middleImage removeTapGesture];
    [banner.container addSubview:banner.middleImage];
    
    banner.rightImage=[KATImage image];
    banner.rightImage.frame=CGRectMake(frame.size.width*2, 0, frame.size.width, frame.size.height);
    banner.rightImage.contentMode=UIViewContentModeScaleAspectFit;
    banner.rightImage.isParsingImageAsync=NO;
    [banner.rightImage removeTapGesture];
    [banner.container addSubview:banner.rightImage];
    
    
    //数据
    banner.dataArray=nil;
    banner.indicatorArray=nil;
    banner.eventDelegate=nil;
    banner.leftImageIndex=-1;
    banner.middleImageIndex=-1;
    banner.rightImageIndex=-1;
    banner.hasCacheImage=NO;
    banner.cacheImage=nil;
    
    
    //前景条
    banner.hasFrontBar=NO;
    banner.frontBar=nil;
    banner.frontBarColor=KCOLOR_SHADOW;
    banner.frontBarFrame=CGRectMake(0, frame.size.height-frame.size.width*0.072, frame.size.width, frame.size.width*0.072);
    
    
    //标题
    banner.hasTitle=NO;
    banner.title=nil;
    banner.titleFrame=CGRectMake(0, 0, frame.size.width, frame.size.width*0.072);
    banner.titleColor=KCOLOR_WHITE;
    banner.titleBgColor=KCOLOR_SHADOW;
    banner.titleFont=[UIFont systemFontOfSize:frame.size.width*0.048];
    banner.titleAlignment=NSTextAlignmentCenter;
    banner.titleLines=1;
    banner.titleLineBreak=NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
    
    
    //指示器
    banner.indicatorColor=[UIColor whiteColor];
    banner.indicatorHeight=frame.size.width*0.025;
    banner.indicatorPaddingInner=frame.size.width*0.025;
    banner.indicatorPaddingBottom=frame.size.width*0.025;
    banner.indicatorLightAlpha=1.0f;
    banner.indicatorDarkAlpha=0.5f;
    banner.indicatorLineWidth=0.5f;
    
    
    banner.padding=20.0;
    banner.scalable=NO;
    
    
    return banner;
}


- (void)setPadding:(float)padding
{
    _padding=padding;
    
    if(_leftImage)
    {
        _leftImage.frame=CGRectMake(-_padding, 0, _leftImage.frame.size.width, _leftImage.frame.size.height);
    }
    
    if(_rightImage)
    {
        _rightImage.frame=CGRectMake(self.frame.size.width*2+_padding, 0, self.frame.size.width, self.frame.size.height);
    }
}


- (void)setScalable:(BOOL)scalable
{
    _scalable=scalable;
    
    if(_scalable)//设置可缩放，则关闭轮播
    {
        _changeMode=BANNER_CHANGE_MODE_NONE;
        
        if(_changeImageTimer)//取消定时器
        {
            [_changeImageTimer invalidate];
            [_changeImageTimer release];
            self.changeImageTimer=nil;
            
            _isChangeImageTimerWorking=NO;
        }
    }
}


//重新加载数据
- (void)reloadData:(KATArray<__kindof KATBannerData *> *)dataArray
{
    if(dataArray && dataArray.length>0)
    {
        self.hidden=NO;
        
        _state=BANNER_STATE_RELOAD;
        
        //归位
        [self restoreScaleAndAnimated:NO];
        
        self.dataArray=dataArray;
        
        if(_remoteImageUpdateTimer)//取消定时器
        {
            [_remoteImageUpdateTimer invalidate];
            self.remoteImageUpdateTimer=nil;
        }
        
        if(_changeImageTimer)//取消定时器
        {
            [_changeImageTimer invalidate];
            
            self.changeImageTimer=nil;
        }
        
        _isRemoteImageUpdateTimerWorking=NO;
        _isChangeImageTimerWorking=NO;
        
        
        //前景条
        if(_hasFrontBar)
        {
            if(!_frontBar)
            {
                self.frontBar=[[[UIView alloc] init] autorelease];
                _frontBar.frame=_frontBarFrame;
                _frontBar.backgroundColor=_frontBarColor;
            }
            
            [self addSubview:_frontBar];
        }
        else
        {
            if(_frontBar)
            {
                [_frontBar removeFromSuperview];
                
                self.frontBar=nil;
            }
        }
        
        
        //清除原先的指示器
        if(_indicatorArray && _indicatorArray.length>0)
        {
            for(int i=0;i<_indicatorArray.length;i++)
            {
                KATButton *indicator=(KATButton *)[_indicatorArray get:i];
                
                [indicator removeFromSuperview];
            }
            
            [_indicatorArray clear];
        }
        
        
        //创建新的指示器数组
        if(_indicatorArray==nil)
        {
            self.indicatorArray=[KATArray arrayWithCapacity:_dataArray.length];
        }
        
        //计算指示器左边距
        float paddingLeft=(self.frame.size.width-(_dataArray.length*_indicatorHeight+(_dataArray.length-1)*_indicatorPaddingInner))*0.5;
        
        //创建指示器
        if(_dataArray.length>1)
        {
            for(int i=0;i<_dataArray.length;i++)
            {
                KATBannerData *data=(KATBannerData *)[_dataArray get:i];
                
                KATButton *indicator=[KATButton buttonWithFrame:CGRectMake(paddingLeft+(_indicatorHeight+_indicatorPaddingInner)*i, self.frame.size.height-_indicatorPaddingBottom-_indicatorHeight, _indicatorHeight, _indicatorHeight) type:data.indicator color:_indicatorColor lineWidth:_indicatorLineWidth scale:1.0 andOnClickAction:nil];
                
                if(i>0)//0号指示器高亮
                {
                    indicator.alpha=_indicatorDarkAlpha;
                }
                
                [self addSubview:indicator];
                [_indicatorArray put:indicator];
            }
        }
        
        //计算图片索引
        _middleImageIndex=0;
        
        if(_isCycleMode)
        {
            if(_dataArray.length>2)
            {
                _leftImageIndex=_dataArray.length-1;
                _rightImageIndex=1;
            }
            else if(_dataArray.length>1)
            {
                _leftImageIndex=1;
                _rightImageIndex=1;
            }
            else
            {
                _leftImageIndex=0;
                _rightImageIndex=0;
            }
        }
        else
        {
            _leftImageIndex=-1;
            _rightImageIndex=1;
        }
        
        KATBannerData *leftData=(KATBannerData *)[_dataArray get:_leftImageIndex];
        KATBannerData *middleData=(KATBannerData *)[_dataArray get:_middleImageIndex];
        KATBannerData *rightData=(KATBannerData *)[_dataArray get:_rightImageIndex];
        
        if(_isCycleMode)//循环模式
        {
            //创建左图
            if(leftData.isImageReady && !leftData.isLoadImageFail)
            {
                if(leftData.imagePath)
                {
                    [_leftImage loadWithPath:leftData.imagePath];
                }
            }
            else
            {
                if(_hasCacheImage && _cacheImage)
                {
                    [_leftImage clearImage];
                    _leftImage.contents=_cacheImage;
                }
                else
                {
                    _leftImage.contents=nil;
                }
            }
            
            //创建中图
            if(middleData.isImageReady && !middleData.isLoadImageFail)
            {
                if(middleData.imagePath)
                {
                    [_middleImage loadWithPath:middleData.imagePath];
                }
            }
            else
            {
                if(_hasCacheImage && _cacheImage)
                {
                    [_middleImage clearImage];
                    _middleImage.contents=_cacheImage;
                }
                else
                {
                    _middleImage.contents=nil;
                }
            }
            
            //创建右图
            if(rightData.isImageReady && !rightData.isLoadImageFail)
            {
                if(rightData.imagePath)
                {
                    [_rightImage loadWithPath:rightData.imagePath];
                }
            }
            else
            {
                if(_hasCacheImage && _cacheImage)
                {
                    [_rightImage clearImage];
                    _rightImage.contents=_cacheImage;
                }
                else
                {
                    _rightImage.contents=nil;
                }
            }
        }
        else
        {
            //左图为空
            _leftImage.contents=nil;
            
            //创建中图
            if(middleData)
            {
                if(middleData.isImageReady && !middleData.isLoadImageFail)
                {
                    if(middleData.imagePath)
                    {
                        [_middleImage loadWithPath:middleData.imagePath];
                    }
                }
                else
                {
                    if(_hasCacheImage && _cacheImage)
                    {
                        [_middleImage clearImage];
                        _middleImage.contents=_cacheImage;
                    }
                    else
                    {
                        _middleImage.contents=nil;
                    }
                }
            }
            else
            {
                _middleImage.contents=nil;
            }
            
            //创建右图
            if(rightData)
            {
                if(rightData.isImageReady && !rightData.isLoadImageFail)
                {
                    if(rightData.imagePath)
                    {
                        [_rightImage loadWithPath:rightData.imagePath];
                    }
                }
                else
                {
                    if(_hasCacheImage && _cacheImage)
                    {
                        [_rightImage clearImage];
                        _rightImage.contents=_cacheImage;
                    }
                    else
                    {
                        _rightImage.contents=nil;
                    }
                }
            }
            else
            {
                _rightImage.contents=nil;
            }
        }
        
        
        //显示标题
        if(_hasTitle)
        {
            if(!_title)
            {
                self.title=[[[UILabel alloc] init] autorelease];
            }
            
            _title.frame=_titleFrame;
            _title.text=middleData.title;
            _title.textColor=_titleColor;
            _title.backgroundColor=_titleBgColor;
            _title.font=_titleFont;
            _title.textAlignment=_titleAlignment;
            _title.numberOfLines=_titleLines;
            _title.lineBreakMode=_titleLineBreak;
            
            if(middleData.hasTitleColor)
            {
                _title.textColor=middleData.titleColor;
            }
            
            [self addSubview:self.title];
        }
        else
        {
            if(_title)
            {
                [_title removeFromSuperview];
                
                self.title=nil;
            }
        }
            
        if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
        {
            _isRemoteImageUpdateTimerWorking=YES;
            self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
        }
        
        if(_changeMode!=BANNER_CHANGE_MODE_NONE && !_isChangeImageTimerWorking && !_scalable)//启动轮播线程
        {
            _isChangeImageTimerWorking=YES;
            self.changeImageTimer=[NSTimer scheduledTimerWithTimeInterval:_changeImageInterval target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
            
            //标记为common modes的模式：UITrackingRunLoopMode和NSDefaultRunLoopMode兼容
            [[NSRunLoop mainRunLoop] addTimer:_changeImageTimer forMode:NSRunLoopCommonModes];
        }
        
        //恢复缩放
        [self restoreScaleAndAnimated:NO];
        
        _state=BANNER_STATE_READY;
    }
    else
    {
        self.hidden=YES;
    }
}


//获取当前数据
- (KATBannerData *)currentData
{
    if(_dataArray && _dataArray.length>0)
    {
        return [_dataArray get:_middleImageIndex];
    }
    
    return nil;
}


//双击事件
- (void)doubleTap
{
    if(_middleImage && _scalable)
    {
        if(_scaled)//已经放大
        {
            [self restoreScaleAndAnimated:YES];
        }
        else if(_middleImage && _middleImage.contents)
        {
            //计算缩放比例
            _scaled=YES;
            _scale=_scaleMax;
            _lastScale=_scale;
            
            [UIView animateWithDuration:0.2 animations:^
            {
                _middleImage.bounds=CGRectMake(0, 0, self.bounds.size.width*_scale, self.bounds.size.height*_scale);
                
                _leftImage.frame=CGRectMake(-_padding-self.bounds.size.width*(_scale-1.0)/2.0, 0, _leftImage.frame.size.width, _leftImage.frame.size.height);
                
                _rightImage.frame=CGRectMake(self.frame.size.width*2+_padding+self.bounds.size.width*(_scale-1.0)/2.0, 0, self.frame.size.width, self.frame.size.height);
            }
            completion:^(BOOL finished)
            {
                if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(banner:didMaximizeWithData:)])
                {
                    [_eventDelegate banner:self didMaximizeWithData:_dataArray[_middleImageIndex]];
                }
            }];
        }
    }
}


//恢复尺寸
- (void)restoreScaleAndAnimated:(BOOL)animated
{
    _scaled=NO;
    _scale=1.0;
    _lastScale=1.0;

    [self updateScaleMax];
    
    if(animated)
    {
        [UIView animateWithDuration:0.2 animations:^
        {
            _container.layer.position=CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
            
            if(_middleImage)
            {
                _middleImage.bounds=self.bounds;
                _middleImage.layer.position=CGPointMake(_container.bounds.size.width/2.0, _container.bounds.size.height/2.0);
            }
            
            if(_leftImage)
            {
                _leftImage.frame=CGRectMake(-_padding, 0, _leftImage.frame.size.width, _leftImage.frame.size.height);
            }
            
            if(_rightImage)
            {
                _rightImage.frame=CGRectMake(self.frame.size.width*2+_padding, 0, self.frame.size.width, self.frame.size.height);
            }
        }];
    }
    else
    {
        _container.layer.position=CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
        
        if(_middleImage)
        {
            _middleImage.bounds=self.bounds;
            _middleImage.layer.position=CGPointMake(_container.bounds.size.width/2.0, _container.bounds.size.height/2.0);
        }
        
        if(_leftImage)
        {
            _leftImage.frame=CGRectMake(-_padding, 0, _leftImage.frame.size.width, _leftImage.frame.size.height);
        }
        
        if(_rightImage)
        {
            _rightImage.frame=CGRectMake(self.frame.size.width*2+_padding, 0, self.frame.size.width, self.frame.size.height);
        }

    }
}


//更新最大缩放尺寸
- (void)updateScaleMax
{
    if(_scalable && _middleImage && _middleImage.contents && _middleImage.source)
    {
        //计算缩放比例
        _scaleMax=_middleImage.source.size.width/self.bounds.size.width>=_middleImage.source.size.height/self.bounds.size.height?_middleImage.source.size.width/self.bounds.size.width:_middleImage.source.size.height/self.bounds.size.height;
        
        if(_scaleMax<_scaleMaxDefault)
        {
            _scaleMax=_scaleMaxDefault;
        }
    }
    else
    {
        _scaleMax=1.0;
    }
}



//捏合事件
- (void)actionPinch:(UIPinchGestureRecognizer *)recognizer
{
    if(_scalable && _middleImage)
    {
        _scale=recognizer.scale*_lastScale;
        
        if(recognizer.state==UIGestureRecognizerStateBegan)//开始
        {
            if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(banner:willScaleWithData:)])
            {
                [_eventDelegate banner:self willScaleWithData:_dataArray[_middleImageIndex]];
            }
        }
        else if(recognizer.state==UIGestureRecognizerStateChanged)//改变状态
        {
            _middleImage.bounds=CGRectMake(0, 0, self.bounds.size.width*_scale, self.bounds.size.height*_scale);
            
            if(_scale>1.0)
            {
                _leftImage.frame=CGRectMake(-_padding-self.bounds.size.width*_scaleMax, 0, _leftImage.frame.size.width, _leftImage.frame.size.height);
            
                _rightImage.frame=CGRectMake(self.frame.size.width*2+_padding+self.bounds.size.width*_scaleMax, 0, self.frame.size.width, self.frame.size.height);
            }
            else
            {
                _leftImage.frame=CGRectMake(-_padding-self.bounds.size.width*_scaleMax, 0, _leftImage.frame.size.width, _leftImage.frame.size.height);
                
                _rightImage.frame=CGRectMake(self.frame.size.width*2+_padding+self.bounds.size.width*_scaleMax, 0, self.frame.size.width, self.frame.size.height);
            }
            
            if(_scale>=0.999999 && _scale<=1.000001)//等于1
            {
                _scaled=NO;
                
                [self restoreScaleAndAnimated:NO];
            }
            else
            {
                _scaled=YES;
            }
        }
        else if(recognizer.state==UIGestureRecognizerStateEnded || recognizer.state==UIGestureRecognizerStateFailed)//结束后，超出边界则恢复状态
        {
            _lastScale=_scale;
            
            if(_scale<1.0)//小于1
            {
                [self restoreScaleAndAnimated:YES];
            }
            else if(_scale>_scaleMax)//大于最大值
            {
                _scaled=YES;
                _scale=_scaleMax;
                _lastScale=_scale;
                
                [UIView animateWithDuration:0.2 animations:^
                {
                    _container.layer.position=CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
                    
                    _middleImage.bounds=CGRectMake(0, 0, self.bounds.size.width*_scale, self.bounds.size.height*_scale);
                    
                    _leftImage.frame=CGRectMake(-_padding-self.bounds.size.width*(_scale-1.0)/2.0, 0, _leftImage.frame.size.width, _leftImage.frame.size.height);
                    
                    _rightImage.frame=CGRectMake(self.frame.size.width*2+_padding+self.bounds.size.width*(_scale-1.0)/2.0, 0, self.frame.size.width, self.frame.size.height);
                }
                completion:^(BOOL finished)
                {
                    if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(banner:didScaleWithData:)])
                    {
                        [_eventDelegate banner:self didScaleWithData:_dataArray[_middleImageIndex]];
                    }
                }];
            }
            else
            {
                [UIView animateWithDuration:0.2 animations:^
                {
                    _container.layer.position=CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
                     
                    _leftImage.frame=CGRectMake(-_padding-self.bounds.size.width*(_scale-1.0)/2.0, 0, _leftImage.frame.size.width, _leftImage.frame.size.height);
                    
                    _rightImage.frame=CGRectMake(self.frame.size.width*2+_padding+self.bounds.size.width*(_scale-1.0)/2.0, 0, self.frame.size.width, self.frame.size.height);
                }
                completion:^(BOOL finished)
                {
                    if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(banner:didScaleWithData:)])
                    {
                        [_eventDelegate banner:self didScaleWithData:_dataArray[_middleImageIndex]];
                    }
                }];
            }
        }
    }
}



//拖动事件
- (void)actionPan:(UIPanGestureRecognizer *)recognizer
{
    if(!_scaled && _dataArray.length<2)
    {
        return;
    }
    
    if((_state==BANNER_STATE_READY || _state==BANNER_STATE_PAN) && _dataArray && _dataArray.length>=1)
    {
        _state=BANNER_STATE_PAN;
        
        CGPoint translation=[recognizer translationInView:self];
        CGFloat x=recognizer.view.center.x+translation.x;
        CGFloat y=recognizer.view.center.y;
        
        if(_scaled)
        {
            y=recognizer.view.center.y+translation.y;
        }
        
        //快速滑动判断
        if(fabsf((float)translation.x)>self.frame.size.width*0.04)
        {
            _isQuicklyPan=YES;
            
            if(translation.x>0)
            {
                _quicklyPanDirection=BANNER_CHANGE_RIGHT;
            }
            else if(translation.x<0)
            {
                _quicklyPanDirection=BANNER_CHANGE_LEFT;
            }
        }
        else if(fabsf((float)translation.x)<self.frame.size.width*0.02 && fabsf((float)translation.x)>self.frame.size.width*0.004)
        {
            _isQuicklyPan=NO;
        }
        
        
        
        if(_scaled)//缩放后边界判定
        {
            if(x<-self.frame.size.width*0.5*_scale)
            {
                x=-self.frame.size.width*0.5*_scale;
            }
            else if(x>self.frame.size.width*1.5*_scale)
            {
                x=self.frame.size.width*1.5*_scale;
            }
            
            if(y<-self.bounds.size.height*0.5*_scale)
            {
                y=-self.bounds.size.height*0.5*_scale;
            }
            
            if(y>self.bounds.size.height*1.5*_scale)
            {
                y=self.bounds.size.height*1.5*_scale;
            }
        }
        else//非缩放
        {
            //边界判断
            if(x<-self.frame.size.width*0.5)
            {
                x=-self.frame.size.width*0.5;
            }
            else if(x>self.frame.size.width*1.5)
            {
                x=self.frame.size.width*1.5;
            }

        }
        
        //改变位置
        recognizer.view.center=CGPointMake(x, y);//改变位置
        [recognizer setTranslation:CGPointMake(0, 0) inView:self];//滑动位置至0
        
        //改变左右图的y坐标
        if(_scaled)
        {
            _leftImage.layer.position=CGPointMake(_leftImage.layer.position.x, (self.bounds.size.height-_container.layer.position.y));
            _rightImage.layer.position=CGPointMake(_rightImage.layer.position.x, (self.bounds.size.height-_container.layer.position.y));
        }
        
        
        if(recognizer.state==UIGestureRecognizerStateBegan)//开始拖拽
        {
            if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(banner:willDragWithData:)])
            {
                [_eventDelegate banner:self willDragWithData:_dataArray[_middleImageIndex]];
            }
        }
        
        //拖拽结束
        if(recognizer.state==UIGestureRecognizerStateEnded || recognizer.state==UIGestureRecognizerStateFailed)
        {
            _state=BANNER_STATE_CHANGE;
            
            float finalX=recognizer.view.center.x;//x坐标最终落点
            float finalY=recognizer.view.center.y;//y坐标最终落点
            int change=BANNER_CHANGE_MIDDLE;//改变的方向
            
            if(_scaled)//缩放后
            {
                if(finalX<self.frame.size.width*0-self.frame.size.width*(_scale-1.0)/2.0)//显示右图(左移)
                {
                    if(_isCycleMode || _middleImageIndex<_dataArray.length-1)
                    {
                        change=BANNER_CHANGE_LEFT;
                        
                        finalX=-self.frame.size.width*0.5-_padding-self.frame.size.width*(_scale-1.0)/2.0;
                        
                        if(_isAutoChangeMode)//自动切换轮播模式
                        {
                            _changeMode=BANNER_CHANGE_MODE_LEFT;
                        }
                    }
                    else//非循环模式且滑动到尾
                    {
                        change=BANNER_CHANGE_MIDDLE;
                        
                        finalX=self.frame.size.width*0.5;
                        
                        _changeMode=BANNER_CHANGE_MODE_RIGHT;
                    }
                }
                else if(finalX>self.frame.size.width+self.frame.size.width*(_scale-1.0)/2.0)//显示左图(右移)
                {
                    if(_isCycleMode || _middleImageIndex>0)
                    {
                        change=BANNER_CHANGE_RIGHT;
                        
                        finalX=self.frame.size.width*1.5+_padding+self.frame.size.width*(_scale-1.0)/2.0;
                        
                        if(_isAutoChangeMode)//自动切换轮播模式
                        {
                            _changeMode=BANNER_CHANGE_MODE_RIGHT;
                        }
                    }
                    else//非循环模式且滑动到头
                    {
                        change=BANNER_CHANGE_MIDDLE;
                        
                        finalX=self.frame.size.width*0.5;
                        
                        _changeMode=BANNER_CHANGE_MODE_RIGHT;
                    }
                }
                else//显示中图
                {
                    change=BANNER_CHANGE_MIDDLE;
                }

            }
            else//未缩放
            {
                if(finalX<self.frame.size.width*0 || (_isQuicklyPan && _quicklyPanDirection==BANNER_CHANGE_LEFT))//显示右图(左移)
                {
                    
                    if(_isCycleMode || _middleImageIndex<_dataArray.length-1)
                    {
                        change=BANNER_CHANGE_LEFT;
                        
                        finalX=-self.frame.size.width*0.5-_padding;
                        
                        if(_isAutoChangeMode)//自动切换轮播模式
                        {
                            _changeMode=BANNER_CHANGE_MODE_LEFT;
                        }
                    }
                    else//非循环模式且滑动到尾
                    {
                        change=BANNER_CHANGE_MIDDLE;
                        
                        finalX=self.frame.size.width*0.5;
                        
                        _changeMode=BANNER_CHANGE_MODE_RIGHT;
                    }
                }
                else if(finalX>self.frame.size.width || (_isQuicklyPan && _quicklyPanDirection==BANNER_CHANGE_RIGHT))//显示左图(右移)
                {
                    if(_isCycleMode || _middleImageIndex>0)
                    {
                        change=BANNER_CHANGE_RIGHT;
                        
                        finalX=self.frame.size.width*1.5+_padding;
                        
                        if(_isAutoChangeMode)//自动切换轮播模式
                        {
                            _changeMode=BANNER_CHANGE_MODE_RIGHT;
                        }
                    }
                    else//非循环模式且滑动到头
                    {
                        change=BANNER_CHANGE_MIDDLE;
                        
                        finalX=self.frame.size.width*0.5;
                        
                        _changeMode=BANNER_CHANGE_MODE_RIGHT;
                    }
                }
                else//显示中图
                {
                    change=BANNER_CHANGE_MIDDLE;
                    
                    finalX=self.frame.size.width*0.5;
                }
            }
            
            
            if(_scaled && change==BANNER_CHANGE_MIDDLE)//缩放后且不需要改变
            {
                //是否缩放后修正
                BOOL modify=NO;
                
                //x坐标修正
                if(finalX>self.frame.size.width*0.5)
                {
                    if(finalX>self.frame.size.width*0.5+self.frame.size.width*(_scale-1.0)/2.0)
                    {
                        finalX=self.frame.size.width*0.5+self.frame.size.width*(_scale-1.0)/2.0;
                        
                        modify=YES;
                    }
                }
                else
                {
                    if(finalX<self.frame.size.width*0.5-self.frame.size.width*(_scale-1.0)/2.0)
                    {
                        finalX=self.frame.size.width*0.5-self.frame.size.width*(_scale-1.0)/2.0;
                        
                        modify=YES;
                    }
                }
                
                //y坐标修正
                if(finalY>self.frame.size.height*0.5)
                {
                    if(finalY>self.frame.size.height*0.5+self.frame.size.height*(_scale-1.0)/2.0)
                    {
                        finalY=self.frame.size.height*0.5+self.frame.size.height*(_scale-1.0)/2.0;
                        
                        modify=YES;
                    }
                }
                else
                {
                    if(finalY<self.frame.size.height*0.5-self.frame.size.height*(_scale-1.0)/2.0)
                    {
                        finalY=self.frame.size.height*0.5-self.frame.size.height*(_scale-1.0)/2.0;
                        
                        modify=YES;
                    }
                }
                
                //移动动画
                [UIView animateWithDuration:MIN(fabs(finalX-_container.center.x)/self.frame.size.width*_animateTime*(_isQuicklyPan?0.6:1.0)+(modify?0.1:0.0),0.4) animations:^
                {
                    //改变位置
                    recognizer.view.center=CGPointMake(finalX, finalY);//改变位置
                    
                    _leftImage.layer.position=CGPointMake(_leftImage.layer.position.x, (self.bounds.size.height-_container.layer.position.y));
                    _rightImage.layer.position=CGPointMake(_rightImage.layer.position.x, (self.bounds.size.height-_container.layer.position.y));
                }
                completion:^(BOOL finished)
                {
                    _isQuicklyPan=NO;
                    _quicklyPanDirection=BANNER_CHANGE_MIDDLE;
                    _state=BANNER_STATE_READY;//改变状态
                }];
            }
            else//需要改变
            {
                //移动动画
                [UIView animateWithDuration:fabs(finalX-_container.center.x)/self.frame.size.width*_animateTime*(_isQuicklyPan?0.6:1.0) animations:^
                {
                    //改变位置
                    recognizer.view.center=CGPointMake(finalX, y);//改变位置
    //                [recognizer setTranslation:CGPointMake(0, 0) inView:self];//滑动位置至0
                }
                completion:^(BOOL finished)
                {
                    if(change!=BANNER_CHANGE_MIDDLE || !_isCycleMode)//左右移动需要改变状态和图片顺序
                    {
                        //计算图片索引
                        if(change==BANNER_CHANGE_LEFT)//左移
                        {
                            _middleImageIndex++;
                            
                            if(_middleImageIndex>_dataArray.length-1)
                            {
                                if(_isCycleMode)
                                {
                                    _middleImageIndex=0;
                                }
                            }
                            
                            _leftImageIndex++;
                            
                            if(_leftImageIndex>_dataArray.length-1)
                            {
                                if(_isCycleMode)
                                {
                                    _leftImageIndex=0;
                                }
                            }
                            
                            _rightImageIndex++;
                            
                            if(_rightImageIndex>_dataArray.length-1)
                            {
                                if(_isCycleMode)
                                {
                                    _rightImageIndex=0;
                                }
                            }
                        }
                        else if(change==BANNER_CHANGE_RIGHT)//右移
                        {
                            _middleImageIndex--;
                            
                            if(_middleImageIndex<0)
                            {
                                if(_isCycleMode)
                                {
                                    _middleImageIndex=_dataArray.length-1;
                                }
                            }
                            
                            _leftImageIndex--;
                            
                            if(_leftImageIndex<0)
                            {
                                if(_isCycleMode)
                                {
                                    _leftImageIndex=_dataArray.length-1;
                                }
                            }
                            
                            _rightImageIndex--;
                            
                            if(_rightImageIndex<0)
                            {
                                if(_isCycleMode)
                                {
                                    _rightImageIndex=_dataArray.length-1;
                                }
                            }
                        }
                        
                        //改变图片
                        KATBannerData *leftData=(KATBannerData *)[_dataArray get:_leftImageIndex];
                        KATBannerData *middleData=(KATBannerData *)[_dataArray get:_middleImageIndex];
                        KATBannerData *rightData=(KATBannerData *)[_dataArray get:_rightImageIndex];
                        
                        
                        if(_isCycleMode)//循环模式
                        {
                            //左图
                            if(leftData.isImageReady && !leftData.isLoadImageFail)
                            {
                                if(leftData.imagePath)
                                {
                                    [_leftImage loadWithPath:leftData.imagePath];
                                }
                            }
                            else
                            {
                                if(_hasCacheImage && _cacheImage)
                                {
                                    [_leftImage clearImage];
                                    _leftImage.contents=_cacheImage;
                                }
                                else
                                {
                                    _leftImage.contents=nil;
                                }
                                
                                if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                                {
                                    _isRemoteImageUpdateTimerWorking=YES;
                                    self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                                }
                            }
                            
                            //中图
                            if(middleData.isImageReady && !middleData.isLoadImageFail)
                            {
                                if(middleData.imagePath)
                                {
                                    [_middleImage loadWithPath:middleData.imagePath];
                                }
                            }
                            else
                            {
                                if(_hasCacheImage && _cacheImage)
                                {
                                    [_middleImage clearImage];
                                    _middleImage.contents=_cacheImage;
                                }
                                else
                                {
                                    _middleImage.contents=nil;
                                }
                                
                                if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                                {
                                    _isRemoteImageUpdateTimerWorking=YES;
                                    self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                                }
                            }
                            
                            //右图
                            if(rightData.isImageReady && !rightData.isLoadImageFail)
                            {
                                if(rightData.imagePath)
                                {
                                    [_rightImage loadWithPath:rightData.imagePath];
                                }
                            }
                            else
                            {
                                if(_hasCacheImage && _cacheImage)
                                {
                                    [_rightImage clearImage];
                                    _rightImage.contents=_cacheImage;
                                }
                                else
                                {
                                    _rightImage.contents=nil;
                                }
                                
                                if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                                {
                                    _isRemoteImageUpdateTimerWorking=YES;
                                    self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                                }
                            }
                        }
                        else//非循环模式
                        {
                            //左图
                            if(leftData)
                            {
                                if(leftData.isImageReady && !leftData.isLoadImageFail)
                                {
                                    if(leftData.imagePath)
                                    {
                                        [_leftImage loadWithPath:leftData.imagePath];
                                    }
                                }
                                else
                                {
                                    if(_hasCacheImage && _cacheImage)
                                    {
                                        [_leftImage clearImage];
                                        _leftImage.contents=_cacheImage;
                                    }
                                    else
                                    {
                                        _leftImage.contents=nil;
                                    }
                                    
                                    if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                                    {
                                        _isRemoteImageUpdateTimerWorking=YES;
                                        self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                                    }
                                }

                            }
                            else
                            {
                                _leftImage.contents=nil;
                            }
                            
                            //中图
                            if(middleData)
                            {
                                if(middleData.isImageReady && !middleData.isLoadImageFail)
                                {
                                    if(middleData.imagePath)
                                    {
                                        [_middleImage loadWithPath:middleData.imagePath];
                                    }
                                }
                                else
                                {
                                    if(_hasCacheImage && _cacheImage)
                                    {
                                        [_middleImage clearImage];
                                        _middleImage.contents=_cacheImage;
                                    }
                                    else
                                    {
                                        _middleImage.contents=nil;
                                    }
                                    
                                    if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                                    {
                                        _isRemoteImageUpdateTimerWorking=YES;
                                        self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                                    }
                                }

                            }
                            else
                            {
                                _middleImage.contents=nil;
                            }
                            
                            
                            //右图
                            if(rightData)
                            {
                                if(rightData.isImageReady && !rightData.isLoadImageFail)
                                {
                                    if(rightData.imagePath)
                                    {
                                        [_rightImage loadWithPath:rightData.imagePath];
                                    }
                                }
                                else
                                {
                                    if(_hasCacheImage && _cacheImage)
                                    {
                                        [_rightImage clearImage];
                                        _rightImage.contents=_cacheImage;
                                    }
                                    else
                                    {
                                        _rightImage.contents=nil;
                                    }
                                    
                                    if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                                    {
                                        _isRemoteImageUpdateTimerWorking=YES;
                                        self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                                    }
                                }
                            }
                            else
                            {
                                _rightImage.contents=nil;
                            }
                        }
                        
                        //归位
                        if(change!=BANNER_CHANGE_MIDDLE)
                        {
                            [self restoreScaleAndAnimated:NO];
                        }
                        
                        //移动图片位置(回到中点)
//                        recognizer.view.center = CGPointMake(self.frame.size.width*0.5, y);//改变位置
    //                    [recognizer setTranslation:CGPointMake(0, 0) inView:self];//滑动位置至0
                        
                        
                        //改变指示器状态
                        for(int i=0;i<_indicatorArray.length;i++)
                        {
                            KATButton *indicator=(KATButton *)[_indicatorArray get:i];
                            
                            if(i==_middleImageIndex)
                            {
                                indicator.alpha=_indicatorLightAlpha;
                            }
                            else
                            {
                                indicator.alpha=_indicatorDarkAlpha;
                            }
                        }
                        
                        
                        //显示标题
                        if(_hasTitle && _title)
                        {
                            _title.text=middleData.title;
                            
                            if(middleData.hasTitleColor)
                            {
                                _title.textColor=middleData.titleColor;
                            }
      
                        }
                        
                    }
                    
                    
                    
                    if(_isChangeImageTimerWorking && _changeImageTimer)//取消轮播定时器
                    {
                        [_changeImageTimer invalidate];
                        self.changeImageTimer=nil;
                        
                        _isChangeImageTimerWorking=NO;
                    }
                    
                    
                    _isQuicklyPan=NO;
                    _quicklyPanDirection=BANNER_CHANGE_MIDDLE;
                    _state=BANNER_STATE_READY;//改变状态
                    
                    
                    if(_dataArray && _dataArray.length>0)
                    {
                        KATBannerData *data=[_dataArray get:_middleImageIndex];
                        
                        if(change!=BANNER_CHANGE_MIDDLE && data && _eventDelegate && [_eventDelegate respondsToSelector:@selector(banner:didChangeWithData:)])
                        {
                            [_eventDelegate banner:self didChangeWithData:data];
                        }
                    }
                    
                    
                    //重新开启轮播定时器
                    if(_changeMode!=BANNER_CHANGE_MODE_NONE && !_isChangeImageTimerWorking && !_scalable)//启动轮播线程
                    {
                        _isChangeImageTimerWorking=YES;
                        self.changeImageTimer=[NSTimer scheduledTimerWithTimeInterval:_changeImageInterval target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
                        
                        //标记为common modes的模式：UITrackingRunLoopMode和NSDefaultRunLoopMode兼容
                        [[NSRunLoop mainRunLoop] addTimer:_changeImageTimer forMode:NSRunLoopCommonModes];
                    }
                    
                }];
            }
        }
    }
}



//改变显示的数据
- (void)changeWithData:(KATBannerData *)data
{
    if(data && [_dataArray hasMember:data] && _middleImageIndex!=[_dataArray indexWithMember:data])
    {
        _middleImageIndex=[_dataArray indexWithMember:data];
        _leftImageIndex=_middleImageIndex-1;
        _rightImageIndex=_middleImageIndex+1;
        
        if(_leftImageIndex<0)
        {
            if(_isCycleMode)
            {
                _leftImageIndex=_dataArray.length-1;
            }
        }
        
        if(_rightImageIndex>_dataArray.length-1)
        {
            if(_isCycleMode)
            {
                _rightImageIndex=0;
            }
        }
        
        KATBannerData *leftData=(KATBannerData *)[_dataArray get:_leftImageIndex];
        KATBannerData *middleData=(KATBannerData *)[_dataArray get:_middleImageIndex];
        KATBannerData *rightData=(KATBannerData *)[_dataArray get:_rightImageIndex];
        
        //左图
        if(leftData)
        {
            if(leftData.isImageReady && !leftData.isLoadImageFail)
            {
                if(leftData.imagePath)
                {
                    [_leftImage loadWithPath:leftData.imagePath];
                }
            }
            else
            {
                if(_hasCacheImage && _cacheImage)
                {
                    [_leftImage clearImage];
                    _leftImage.contents=_cacheImage;
                }
                else
                {
                    _leftImage.contents=nil;
                }
                
                if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                {
                    _isRemoteImageUpdateTimerWorking=YES;
                    self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                }
            }
            
        }
        else
        {
            _leftImage.contents=nil;
        }
        
        //中图
        if(middleData)
        {
            if(middleData.isImageReady && !middleData.isLoadImageFail)
            {
                if(middleData.imagePath)
                {
                    [_middleImage loadWithPath:middleData.imagePath];
                }
            }
            else
            {
                if(_hasCacheImage && _cacheImage)
                {
                    [_middleImage clearImage];
                    _middleImage.contents=_cacheImage;
                }
                else
                {
                    _middleImage.contents=nil;
                }
                
                if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                {
                    _isRemoteImageUpdateTimerWorking=YES;
                    self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                }
            }
            
        }
        else
        {
            _middleImage.contents=nil;
        }
        
        
        //右图
        if(rightData)
        {
            if(rightData.isImageReady && !rightData.isLoadImageFail)
            {
                if(rightData.imagePath)
                {
                    [_rightImage loadWithPath:rightData.imagePath];
                }
            }
            else
            {
                if(_hasCacheImage && _cacheImage)
                {
                    [_rightImage clearImage];
                    _rightImage.contents=_cacheImage;
                }
                else
                {
                    _rightImage.contents=nil;
                }
                
                if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                {
                    _isRemoteImageUpdateTimerWorking=YES;
                    self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                }
            }
        }
        else
        {
            _rightImage.contents=nil;
        }
        
        //归位
        [self restoreScaleAndAnimated:NO];
        
        //改变指示器状态
        for(int i=0;i<_indicatorArray.length;i++)
        {
            KATButton *indicator=(KATButton *)[_indicatorArray get:i];
            
            if(i==_middleImageIndex)
            {
                indicator.alpha=_indicatorLightAlpha;
            }
            else
            {
                indicator.alpha=_indicatorDarkAlpha;
            }
        }
        
        
        //显示标题
        if(_hasTitle && _title)
        {
            _title.text=middleData.title;
            
            if(middleData.hasTitleColor)
            {
                _title.textColor=middleData.titleColor;
            }
            
        }

        if(_isChangeImageTimerWorking && _changeImageTimer)//取消轮播定时器
        {
            [_changeImageTimer invalidate];
            self.changeImageTimer=nil;
            
            _isChangeImageTimerWorking=NO;
        }
        
        
        _isQuicklyPan=NO;
        _quicklyPanDirection=BANNER_CHANGE_MIDDLE;
        _state=BANNER_STATE_READY;//改变状态
        
        
        //重新开启轮播定时器
        if(_changeMode!=BANNER_CHANGE_MODE_NONE && !_isChangeImageTimerWorking && !_scalable)//启动轮播线程
        {
            _isChangeImageTimerWorking=YES;
            self.changeImageTimer=[NSTimer scheduledTimerWithTimeInterval:_changeImageInterval target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
            
            //标记为common modes的模式：UITrackingRunLoopMode和NSDefaultRunLoopMode兼容
            [[NSRunLoop mainRunLoop] addTimer:_changeImageTimer forMode:NSRunLoopCommonModes];
        }

    }
}


//自动轮播
- (void)changeImage
{
    if(_state==BANNER_STATE_READY && !_stoped)
    {
        if(_changeMode!=BANNER_CHANGE_MODE_NONE && _dataArray && _dataArray.length>1)
        {
            _state=BANNER_STATE_CHANGE;
            
            float finalX=0.0f;//x坐标最终落点
            int change=BANNER_CHANGE_MIDDLE;//改变的方向
            
            
            if(_changeMode==BANNER_CHANGE_MODE_LEFT)//显示右图(左移)
            {
                if(_isCycleMode || _middleImageIndex<_dataArray.length-1)
                {
                    change=BANNER_CHANGE_LEFT;
                    
                    finalX=-self.frame.size.width*0.5-_padding;
                }
                else//非循环模式且滑动到尾
                {
                    change=BANNER_CHANGE_RIGHT;
                    
                    finalX=self.frame.size.width*1.5+_padding;
                    
                    _changeMode=BANNER_CHANGE_MODE_RIGHT;
                }
            }
            else if(_changeMode==BANNER_CHANGE_MODE_RIGHT)//显示左图(右移)
            {
                if(_isCycleMode || _middleImageIndex>0)
                {
                    change=BANNER_CHANGE_RIGHT;
                    
                    finalX=self.frame.size.width*1.5+_padding;
                }
                else//非循环模式且移动到头
                {
                    change=BANNER_CHANGE_LEFT;
                    
                    finalX=-self.frame.size.width*0.5-_padding;
                    
                    _changeMode=BANNER_CHANGE_MODE_LEFT;
                }
            }
            
            //移动动画
            [UIView animateWithDuration:_animateTime animations:^
            {
                //改变位置
                _container.center = CGPointMake(finalX, _container.center.y);//改变位置
            }
            completion:^(BOOL finished)
            {
                if(change!=BANNER_CHANGE_MIDDLE)//左右移动需要改变状态和图片顺序
                {
                    //计算图片索引
                    if(change==BANNER_CHANGE_LEFT)//左移
                    {
                        _middleImageIndex++;
                        
                        if(_middleImageIndex>_dataArray.length-1)
                        {
                            if(_isCycleMode)
                            {
                                _middleImageIndex=0;
                            }
                        }
                        
                        _leftImageIndex++;
                        
                        if(_leftImageIndex>_dataArray.length-1)
                        {
                            if(_isCycleMode)
                            {
                                _leftImageIndex=0;
                            }
                        }
                         
                        _rightImageIndex++;
                        
                        if(_rightImageIndex>_dataArray.length-1)
                        {
                            if(_isCycleMode)
                            {
                                _rightImageIndex=0;
                            }
                        }
                    }
                    else if(change==BANNER_CHANGE_RIGHT)//右移
                    {
                        _middleImageIndex--;
                        
                        if(_middleImageIndex<0)
                        {
                            if(_isCycleMode)
                            {
                                _middleImageIndex=_dataArray.length-1;
                            }
                        }
                        
                        _leftImageIndex--;
                        
                        if(_leftImageIndex<0)
                        {
                            if(_isCycleMode)
                            {
                                _leftImageIndex=_dataArray.length-1;
                            }
                        }
                        
                        _rightImageIndex--;
                        
                        if(_rightImageIndex<0)
                        {
                            if(_isCycleMode)
                            {
                                _rightImageIndex=_dataArray.length-1;
                            }
                        }
                    }
                     
                    //改变图片
                    KATBannerData *leftData=(KATBannerData *)[_dataArray get:_leftImageIndex];
                    KATBannerData *middleData=(KATBannerData *)[_dataArray get:_middleImageIndex];
                    KATBannerData *rightData=(KATBannerData *)[_dataArray get:_rightImageIndex];
                    
                    if(_isCycleMode)//循环模式
                    {
                        //左图
                        if(leftData.isImageReady && !leftData.isLoadImageFail)
                        {
                            if(leftData.imagePath)
                            {
                                [_leftImage loadWithPath:leftData.imagePath];
                            }
                        }
                        else
                        {
                            if(_hasCacheImage && _cacheImage)
                            {
                                [_leftImage clearImage];
                                _leftImage.contents=_cacheImage;
                            }
                            else
                            {
                                _leftImage.contents=nil;
                            }
                            
                            if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                            {
                                _isRemoteImageUpdateTimerWorking=YES;
                                self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                            }
                        }
                        
                        //中图
                        if(middleData.isImageReady && !middleData.isLoadImageFail)
                        {
                            if(middleData.imagePath)
                            {
                                [_middleImage loadWithPath:middleData.imagePath];
                            }
                        }
                        else
                        {
                            if(_hasCacheImage && _cacheImage)
                            {
                                [_middleImage clearImage];
                                _middleImage.contents=_cacheImage;
                            }
                            else
                            {
                                _middleImage.contents=nil;
                            }
                            
                            if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                            {
                                
                                _isRemoteImageUpdateTimerWorking=YES;
                                self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                            }
                        }
                         
                        //右图
                        if(rightData.isImageReady && !rightData.isLoadImageFail)
                        {
                            if(rightData.imagePath)
                            {
                                [_rightImage loadWithPath:rightData.imagePath];
                            }
                        }
                        else
                        {
                            if(_hasCacheImage && _cacheImage)
                            {
                                [_rightImage clearImage];
                                _rightImage.contents=_cacheImage;
                            }
                            else
                            {
                                _rightImage.contents=nil;
                            }
                            
                            if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                            {
                                _isRemoteImageUpdateTimerWorking=YES;
                                self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                            }
                        }
                    }
                    else//非循环模式
                    {
                        //左图
                        if(leftData)
                        {
                            if(leftData.isImageReady && !leftData.isLoadImageFail)
                            {
                                if(leftData.imagePath)
                                {
                                    [_leftImage loadWithPath:leftData.imagePath];
                                }
                            }
                            else
                            {
                                if(_hasCacheImage && _cacheImage)
                                {
                                    [_leftImage clearImage];
                                    _leftImage.contents=_cacheImage;
                                }
                                else
                                {
                                    _leftImage.contents=nil;
                                }
                                
                                if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                                {
                                    _isRemoteImageUpdateTimerWorking=YES;
                                    self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                                }
                            }
                            
                        }
                        else
                        {
                            _leftImage.contents=nil;
                        }
                        
                        //中图
                        if(middleData)
                        {
                            if(middleData.isImageReady && !middleData.isLoadImageFail)
                            {
                                if(middleData.imagePath)
                                {
                                    [_middleImage loadWithPath:middleData.imagePath];
                                }
                            }
                            else
                            {
                                if(_hasCacheImage && _cacheImage)
                                {
                                    [_middleImage clearImage];
                                    _middleImage.contents=_cacheImage;
                                }
                                else
                                {
                                    _middleImage.contents=nil;
                                }
                                
                                if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                                {
                                    _isRemoteImageUpdateTimerWorking=YES;
                                    self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                                }
                            }
                            
                        }
                        else
                        {
                            _middleImage.contents=nil;
                        }
                        
                        
                        //右图
                        if(rightData)
                        {
                            if(rightData.isImageReady && !rightData.isLoadImageFail)
                            {
                                if(rightData.imagePath)
                                {
                                    [_rightImage loadWithPath:rightData.imagePath];
                                }
                            }
                            else
                            {
                                if(_hasCacheImage && _cacheImage)
                                {
                                    [_rightImage clearImage];
                                    _rightImage.contents=_cacheImage;
                                }
                                else
                                {
                                    _rightImage.contents=nil;
                                }
                                
                                if(_hasRemoteImage && !_isRemoteImageUpdateTimerWorking)//启动远程图片线程
                                {
                                    _isRemoteImageUpdateTimerWorking=YES;
                                    self.remoteImageUpdateTimer=[NSTimer scheduledTimerWithTimeInterval:_remoteImageUpdateInterval target:self selector:@selector(remoteImageUpdate) userInfo:nil repeats:YES];
                                }
                            }
                        }
                        else
                        {
                            _rightImage.contents=nil;
                        }
                    }
                    
                    
                    //归位
                    if(change!=BANNER_CHANGE_MIDDLE)
                    {
                        [self restoreScaleAndAnimated:NO];
                    }
                    
                
                    //移动图片位置(回到中点)
                    _container.center = CGPointMake(self.frame.size.width*0.5, _container.center.y);//改变位置
                    
                    //改变指示器状态
                    for(int i=0;i<_indicatorArray.length;i++)
                    {
                        KATButton *indicator=(KATButton *)[_indicatorArray get:i];
                        
                        if(i==_middleImageIndex)
                        {
                            indicator.alpha=_indicatorLightAlpha;
                        }
                        else
                        {
                            indicator.alpha=_indicatorDarkAlpha;
                        }
                    }
                    
                    
                    //显示标题
                    if(_hasTitle && _title)
                    {
                        _title.text=middleData.title;
                    
                        if(middleData.hasTitleColor)
                        {
                            _title.textColor=middleData.titleColor;
                        }
                         
                    }
                    
                    
                    if(_dataArray && _dataArray.length>0)
                    {
                        KATBannerData *data=[_dataArray get:_middleImageIndex];
                        
                        if(data && _eventDelegate && [_eventDelegate respondsToSelector:@selector(banner:didChangeWithData:)])
                        {
                            [_eventDelegate banner:self didChangeWithData:data];
                        }
                    }
                    
                }
                
                _state=BANNER_STATE_READY;
            }];
        }
    }
}



//远程图片加载更新扫描
- (void)remoteImageUpdate
{
    BOOL finish=NO;//完成标记
    
    //改变图片
    KATBannerData *leftData=(KATBannerData *)[_dataArray get:_leftImageIndex];
    KATBannerData *middleData=(KATBannerData *)[_dataArray get:_middleImageIndex];
    KATBannerData *rightData=(KATBannerData *)[_dataArray get:_rightImageIndex];
    
    //左图
    if(leftData.isImageReady && !leftData.isLoadImageFail && (_leftImage.contents==nil || _leftImage.contents==_cacheImage))
    {
        if(leftData.imagePath)
        {
            [_leftImage loadWithPath:leftData.imagePath];
        }
    }
    
    //中图
    if(middleData.isImageReady && !middleData.isLoadImageFail && (_middleImage.contents==nil || _middleImage.contents==_cacheImage))
    {
        if(middleData.imagePath)
        {
            [_middleImage loadWithPath:middleData.imagePath];
        }
    }
    
    //右图
    if(rightData.isImageReady && !rightData.isLoadImageFail && (_rightImage.contents==nil || _rightImage.contents==_cacheImage))
    {
        if(rightData.imagePath)
        {
            [_rightImage loadWithPath:rightData.imagePath];
        }
    }

    
    //三图加载完成
    if(leftData.isImageReady && middleData.isImageReady && rightData.isImageReady)
    {
        finish=YES;
    }
    
    
    if(finish && _isRemoteImageUpdateTimerWorking && _remoteImageUpdateTimer)//取消定时器
    {
        [_remoteImageUpdateTimer invalidate];
        self.remoteImageUpdateTimer=nil;
        
        _isRemoteImageUpdateTimerWorking=NO;
    }
}


//点击事件
-(void)imageTap:(UITapGestureRecognizer *)recognizer
{
    if(_scalable && _doubleClickCheckTime>0)//可以缩放才有双击事件
    {
        if(_isTaped)//已经点击过，执行双击事件
        {
            _isTaped=NO;
            
            [self doubleTap];
        }
        else//单击事件
        {
            _isTaped=YES;
            
            NSTimer *tapTimer=[NSTimer scheduledTimerWithTimeInterval:_doubleClickCheckTime target:self selector:@selector(singleTap) userInfo:nil repeats:NO];
            
            //标记为common modes的模式：UITrackingRunLoopMode和NSDefaultRunLoopMode兼容
            [[NSRunLoop mainRunLoop] addTimer:tapTimer forMode:NSRunLoopCommonModes];
        }
    }
    else//直接执行单击事件
    {
        _isTaped=YES;
        
        [self singleTap];
    }
    
}


//单击事件
- (void)singleTap
{
    if(_isTaped)//没有产生双击事件
    {
        _isTaped=NO;
        
        KATBannerData *data=[_dataArray get:_middleImageIndex];
        
        if(data && _eventDelegate && [_eventDelegate respondsToSelector:@selector(banner:didTapWithData:)])
        {
            [_eventDelegate banner:self didTapWithData:data];
        }
    }
}



//子控件超出部分的事件响应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view=[self.container hitTest:point withEvent:event];
    
    if(view==nil && _middleImage)//不在范围内，检查是否中图在范围内
    {
        // 转换坐标系
        CGPoint newPoint=[self.middleImage convertPoint:point fromView:self];
        
        //判断触摸点是否在中图上
        if(CGRectContainsPoint(self.middleImage.bounds, newPoint))
        {
            view=self.container;
        }
    }
    
    return view;
}


//从父视图中移除时，关闭定时器
- (void)removeFromSuperview
{
    [super removeFromSuperview];
    
    if(_remoteImageUpdateTimer)//取消定时器
    {
        [_remoteImageUpdateTimer invalidate];
        [_remoteImageUpdateTimer release];
        _remoteImageUpdateTimer=nil;
        
        _isRemoteImageUpdateTimerWorking=NO;
    }
    
    if(_changeImageTimer)//取消定时器
    {
        [_changeImageTimer invalidate];
        [_changeImageTimer release];
        _changeImageTimer=nil;
        
        _isChangeImageTimerWorking=NO;
    }
}


- (void)dealloc
{
//    NSLog(@"Banner is delloc!!");
    
    if(_remoteImageUpdateTimer)//取消定时器
    {
        [_remoteImageUpdateTimer invalidate];
        [_remoteImageUpdateTimer release];
        _remoteImageUpdateTimer=nil;
        
        _isRemoteImageUpdateTimerWorking=NO;
    }
    
    if(_changeImageTimer)//取消定时器
    {
        [_changeImageTimer invalidate];
        [_changeImageTimer release];
        _changeImageTimer=nil;
        
        _isChangeImageTimerWorking=NO;
    }
    
    [_container release];
    [_leftImage release];
    [_rightImage release];
    [_middleImage release];
    [_cacheImage release];
    [_dataArray release];
    [_indicatorArray release];
    [_title release];
    [_titleColor release];
    [_titleBgColor release];
    [_titleFont release];
    [_indicatorColor release];
    [_cacheImage release];
    [_frontBar release];
    [_frontBarColor release];
    
    [super dealloc];
}




@end
